package com.ly.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.entity.Activity;
import com.ly.entity.JoinActivity;
import com.ly.entity.Student;
import com.ly.mapper.ActivityMapper;
import com.ly.mapper.JoinActivityMapper;
import com.ly.mapper.StudentMapper;
import com.ly.pojo.dto.JoinActivityDTO;
import com.ly.pojo.vo.JoinActivityVO;
import com.ly.service.JoinActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : 黑渊白花
 * @ClassName JoinActivityServiceImpl
 * @date : 2025/2/8 22:32
 * @Description
 */
@Service
@Slf4j
public class JoinActivityServiceImpl extends ServiceImpl<JoinActivityMapper, JoinActivity> implements JoinActivityService {
    @Autowired
    private JoinActivityMapper joinActivityMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public IPage<JoinActivityVO> getAllJoinActPage(JoinActivityDTO joinActivityDTO) {
        Integer pageNum = joinActivityDTO.getPageNum();
        Integer pageSize = joinActivityDTO.getPageSize();
        String stuName = joinActivityDTO.getStuName();
        String actName = joinActivityDTO.getActName();
        String teamName = joinActivityDTO.getTeamName();
        IPage<JoinActivityVO> page = new Page<>(pageNum,pageSize);
        IPage<JoinActivityVO> allPage = joinActivityMapper.getAllPage(page,teamName,stuName,actName);
        return allPage;
    }

    @Override
    public IPage<JoinActivityVO> getJoinActPage(JoinActivityDTO joinActivityDTO) {
        Integer pageNum = joinActivityDTO.getPageNum();
        Integer pageSize = joinActivityDTO.getPageSize();
        String stuName = joinActivityDTO.getStuName();
        String actName = joinActivityDTO.getActName();
        Integer teamId = joinActivityDTO.getTeamId();
        IPage<JoinActivityVO> page = new Page<>(pageNum,pageSize);
        IPage<JoinActivityVO> stPage = joinActivityMapper.getPage(page,teamId,stuName,actName);
        return stPage;
    }

    @Override
    public IPage<JoinActivityVO> getJoinActPageById(JoinActivityDTO joinActivityDTO) {
        Integer pageNum = joinActivityDTO.getPageNum();
        Integer pageSize = joinActivityDTO.getPageSize();
        String stuName = joinActivityDTO.getStuName();
        Integer actId = joinActivityDTO.getActId();
        IPage<JoinActivityVO> page = new Page<>(pageNum,pageSize);
        IPage<JoinActivityVO> pageById = joinActivityMapper.getPageById(page,stuName,actId);
        return pageById;
    }

    @Override
    public Boolean isJoinThisAct(JoinActivityDTO joinActivityDTO) {
        QueryWrapper<JoinActivity> qw = new QueryWrapper<>();
        qw.eq("stu_id",joinActivityDTO.getStuId());
        qw.eq("act_id",joinActivityDTO.getActId());
        Long count = joinActivityMapper.selectCount(qw);
        if(count>0){
            return true;
        }
        return false;

    }

    //
    @Override
    public Boolean saveJoinAct(JoinActivityVO joinActivityVO) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("name",joinActivityVO.getStuName());
        Student student = studentMapper.selectOne(qw);
        //判断学生是否存在
        if(student==null){
            return false;
        }
        QueryWrapper<JoinActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id",student.getId());
        queryWrapper.eq("act_id",joinActivityVO.getActId());
        //判断学生是否已经报名
        Long count = joinActivityMapper.selectCount(queryWrapper);
        if(count==1){
            return false;
        }
        //判断活动人数是否已经满了
        QueryWrapper qwAct=new QueryWrapper();
        qwAct.eq("act_id",joinActivityVO.getActId());
        Activity activity = activityMapper.selectOne(qwAct);
        if(activity.getPeopleNum()==activity.getEnterNum()){
            return false;
        }
        //添加并活动人数加+1
        joinActivityVO.setStuId(student.getId());
        JoinActivity joinActivity= new JoinActivity();
        BeanUtil.copyProperties(joinActivityVO,joinActivity);
        int insert = joinActivityMapper.insert(joinActivity);
        if(insert==0){
            return false;
        }
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",joinActivity.getActId());
        wrapper.setSql("enter_num = enter_num + 1");
        activityMapper.update(wrapper);
        return true;
    }


    @Override
    public String signAct(JoinActivity joinActivity) {
        // QueryWrapper<JoinActivity> queryWrapper = new QueryWrapper<>();
        // queryWrapper.eq("stu_id",joinActivity.getStuId());
        // queryWrapper.eq("act_id",joinActivity.getActId());
        // //判断学生是否已经报名
        // Long count = joinActivityMapper.selectCount(queryWrapper);
        // if(count==1){
        //     return "用户已经报名";
        // }
        //添加再活动人数+1
        joinActivityMapper.insert(joinActivity);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",joinActivity.getActId());
        wrapper.setSql("enter_num = enter_num + 1");
        activityMapper.update(wrapper);
        return "活动报名成功";
    }

    @Override
    public Boolean deleteJoinAct(JoinActivity joinActivity) {
        QueryWrapper<JoinActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id",joinActivity.getStuId());
        queryWrapper.eq("act_id",joinActivity.getActId());
        JoinActivity joinActivity1 = joinActivityMapper.selectOne(queryWrapper);
        if(joinActivity1==null){
            return false;
        }
        joinActivityMapper.deleteById(joinActivity1);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",joinActivity.getActId());
        wrapper.setSql("enter_num = enter_num - 1");
        activityMapper.update(wrapper);
        return true;
    }

    @Override
    public Boolean delByid(Integer id) {
        JoinActivity joinActivity = joinActivityMapper.selectById(id);
        if(joinActivity==null){
            return false;
        }
        joinActivityMapper.deleteById(id);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",joinActivity.getActId());
        wrapper.setSql("enter_num = enter_num - 1");
        activityMapper.update(wrapper);
        return true;
    }

    @Override
    public List<JoinActivityVO> getAll(Integer actId) {
        return joinActivityMapper.getAll(actId);
    }


}
